package com.logictech.activity.system.util;

import com.logictech.activity.system.entity.MenuFunction;
import com.logictech.activity.system.entity.TreeNode;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;

public class SystemUtil {
	public static String list2TreeWithCheck(List<MenuFunction> allList, List<MenuFunction> roleList) {
		if ((allList != null) && (allList.size() > 0)) {
			List<TreeNode> treeList = new ArrayList();
			for (MenuFunction auth : allList) {
				TreeNode tn = new TreeNode();
				tn.setId(auth.getAuthId());
				String pId = "0";
				if ((auth.getParentAuthId() != null) && (!auth.getParentAuthId().equals(""))) {
					pId = auth.getParentAuthId();
				}
				tn.setpId(pId);
				tn.setName(auth.getAuthName());
				if (("1".equals(auth.getAuthType())) || ("Y".equals(auth.getLeafInd()))) {
					tn.setOpen(false);
				} else {
					tn.setOpen(true);
				}
				tn.setChecked(false);

				for (MenuFunction mf : roleList) {
					if (mf.getAuthId().equals(auth.getAuthId())) {
						tn.setChecked(true);
						break;
					}
				}

				tn.setDoCheck(false);
				tn.setHalfCheck(false);
				tn.setParent(false);
				tn.setChkDisabled(false);
				tn.setNocheck(false);
				treeList.add(tn);
			}
			JSONArray jsonArray = JSONArray.fromObject(treeList);
			return jsonArray.toString();
		}
		return "";
	}

	public static String listTreeToAuth(List<MenuFunction> allList) {
		if ((allList != null) && (allList.size() > 0)) {
			List<TreeNode> treeList = new ArrayList();
			for (MenuFunction auth : allList) {
				TreeNode tn = new TreeNode();
				tn.setId(auth.getAuthId());
				String pId = "0";
				if ((auth.getParentAuthId() != null) && (!auth.getParentAuthId().equals(""))) {
					pId = auth.getParentAuthId();
				}
				tn.setpId(pId);
				tn.setName(auth.getAuthName());
				if (("1".equals(auth.getAuthType())) || ("Y".equals(auth.getLeafInd()))) {
					tn.setOpen(false);
				} else {
					tn.setOpen(true);
				}
				tn.setChecked(false);
				tn.setDoCheck(false);
				tn.setHalfCheck(false);
				tn.setParent(false);
				tn.setChkDisabled(false);
				tn.setNocheck(false);
				treeList.add(tn);
			}
			JSONArray jsonArray = JSONArray.fromObject(treeList);
			return jsonArray.toString();
		}
		return "";
	}
}